package org.schemeway.plugins.schemescript.action;

import org.eclipse.jface.text.*;
import org.schemeway.plugins.schemescript.editor.*;
/**
 * @author Nu Echo Inc.
 */
public class CompressSpacesAction extends SchemeAction {

	public CompressSpacesAction(SchemeEditor editor) {
		super(editor);
		setText("Compress spaces");
		setToolTipText("Replaces all surrounding whitespaces by a single space");
	}

	public void run() {
		SchemeEditor editor = getSchemeEditor();
		if (editor == null)
			return;

		IDocument document = editor.getDocument();
		try {
			int currentPoint = editor.getPoint();
			int docLength = document.getLength();
			int start = currentPoint;
			int end = currentPoint;
			while (start > 0 && Character.isWhitespace(document.getChar(start - 1)))
				start--;
			while (end < docLength && Character.isWhitespace(document.getChar(end)))
				end++;
			editor.replaceText(start, end - start, " ");
		}
		catch (BadLocationException exception) {
		}
	}

}
